package io.renren.modules.sp.controller.front;

import io.renren.common.utils.Result;
import io.renren.modules.sp.dto.SpTeamAttachDTO;
import io.renren.modules.sp.entity.SpTeamAttachEntity;
import io.renren.modules.sp.service.SpTeamAttachService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;


/**
* 代表队附件表（承诺书）
*
* @author whc
* @since 3.0 2025-02-16
*/
@RestController
@RequestMapping("front/spteamattach")
@Api(tags="代表队附件表（承诺书）")
public class FrontTeamAttachController {
    @Autowired
    private SpTeamAttachService spTeamAttachService;

    @GetMapping("info")
    public Result<SpTeamAttachEntity> page(@ApiIgnore @RequestParam Map<String, Object> params){
        List<SpTeamAttachEntity> list = spTeamAttachService.queryByMap(params);
        if(list.size() > 0){
            return new Result<SpTeamAttachEntity>().ok(list.get(0));
        }
        return new Result<SpTeamAttachEntity>().error("未找到数据");
    }

    @GetMapping("save")
    public Result save(@RequestParam Map<String, Object> params){
        String teamId = (String) params.get("teamId");
        String matchId = (String) params.get("matchId");
        String promissFile = (String) params.get("promissFile");
        List<SpTeamAttachEntity> list = spTeamAttachService.queryByMap(params);
        if(list.size() > 0){
            SpTeamAttachEntity entity = list.get(0);
            if(entity!=null){
                entity.setPromissFile(promissFile);
                spTeamAttachService.updateById(entity);
                return new Result().ok("保存成功");
            }
        }else{
            //保存数据
            SpTeamAttachDTO dto = new SpTeamAttachDTO();
            dto.setTeamId(Integer.parseInt(teamId));
            dto.setMatchId(Integer.parseInt(matchId));
            dto.setPromissFile(promissFile);
            spTeamAttachService.save(dto);
        }

        return new Result().ok("保存成功");
    }

    //报名截止之后开赛之前，每次登录，检测有比赛没有上传承诺书，弹窗显示"上传xxx比赛的承诺书"，并跳转到上传承诺书页面
    /**
     * 获取代表队附件表（承诺书）
     * @param teamId
     * @return
     */
    @GetMapping("hasUpPromissFile")
    @ApiOperation("获取代表队附件表（承诺书）")
    public Result<List<Map<String,Object>>> hasUpPromissFile(@RequestParam Long teamId){
        List<Map<String,Object>> list = spTeamAttachService.hasUpPromissFile(teamId);
        return new Result<List<Map<String,Object>>>().ok(list);
    }

    /**
     * 直通时无上传报名表，登录代表队时需弹框提示
     */
    @GetMapping("hasUpSignFile")
    @ApiOperation("获取代表队附件表（报名表）")
    public Result<List<Map<String,Object>>> hasUpSignFile(@RequestParam Long teamId){
        List<Map<String,Object>> list = spTeamAttachService.hasUpSignFile(teamId);
        return new Result<List<Map<String,Object>>>().ok(list);
    }

}
